using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using Models.APIModels;
using Models.ServerModels;

namespace DdnsServer.DnspodAPI
{
    public class RecordApi : APIBase
    {
        public RecordApi(ApiConfigModel cfg) : base(cfg)
        { }
        #region 增加记录
        public AddRecordRetMsgModel AddRecord(string value,int domain_id=-1,string domain="",string record_line_id="",string record_line="默认",string sub_domain="@",string record_type="A",int mx=0,int ttl=600,string status="enable",string weight="")
        {
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            if(string.IsNullOrWhiteSpace(value))
                throw new Exception("记录值不能为空");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["value"] = value,
                ["record_line"] = record_line,
                ["sub_domain"]=sub_domain,
                ["record_type"]=record_type,
                ["mx"]=mx,
                ["ttl"]=ttl,
                ["status"]=status,
                ["weight"]=weight
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            if (record_line_id != "")
                dc["record_line_id"] = record_line_id;
            return PostData<AddRecordRetMsgModel>("Record.Create", dc);

        }
        #endregion
        #region 记录列表
        public RecordListModel GetRecordList(int domain_id = -1, string domain = "",string keyword="", int offset = 0, int length = 3000, string sub_domain = "", string record_type = "", string record_line = "", string record_line_id = "")
        {
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["offset"] = offset,
                ["length"] = length
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            if (keyword != "")
                dc["keyword"] = keyword;
            if (sub_domain!= "")
                dc["sub_domain"] = sub_domain;
            if (record_type != "")
                dc["record_type"] = record_type;
            if (record_line != "")
                dc["record_line"] = record_line;
            if (record_line_id != "")
                dc["record_line_id"] = record_line_id;
            return PostData<RecordListModel>("Record.List", dc);
            
        }
        #endregion
        #region 修改记录
        public EditRecordModel EditRecord(string value,int record_id=-1,string sub_domain="@",int domain_id=-1,string domain="",string record_type="A",string record_line="默认",string record_line_id="",int mx=0,int ttl=600,string status="enable",string weight="")
        {
            if(string.IsNullOrWhiteSpace(value))
                throw new Exception("记录值不能为空");
            if(record_id<=0)
                throw new Exception("记录ID必须指定");
            if(domain_id==-1 && domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["value"] = value,
                ["record_id"] = record_id,
                ["sub_domain"] = sub_domain,
                ["record_type"] = record_type,
                ["record_line"] = record_line,
                ["mx"] = mx,
                ["ttl"] = ttl,
                ["status"] = status,
                ["weight"] = weight
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            if (record_line_id != "")
                dc["record_line_id"] = record_line_id;
            return PostData<EditRecordModel>("Record.Modify", dc);
        }
        #endregion
        #region 删除记录
        public RetMessageModel DelRecord(int record_id,int domain_id=-1,string domain="")
        {
            if(record_id<=0)
                throw new Exception("必须指定记录ID");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object> { ["record_id"] = record_id };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Record.Remove", dc);
        }
        #endregion
        #region 更新Ddns
        public UpdateDdnsRetModel UpdateDdns(string value,int record_id,int domain_id=-1,string domain="",string record_line="默认",string record_line_id="",string sub_domain="@")
        {
            if(string.IsNullOrWhiteSpace(value))
                throw new Exception("记录值不能为空！");
            if(record_id<=0)
                throw new Exception("记录id不能为空");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["value"] = value,
                ["record_id"] = record_id,
                ["record_line"] = record_line,
                ["sub_domain"] = sub_domain
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            if (record_line_id != "")
                dc["record_line_id"] = record_line_id;
            return PostData<UpdateDdnsRetModel>("Record.Ddns", dc);
        }
        #endregion
        #region 设置记录备注
        public RetMessageModel SetRecordRemark(int record_id,string remark,int domain_id=-1,string domain="")
        {
            if(record_id <=0)
                throw new Exception("必须指定记录ID");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["record_id"] = record_id,
                ["remark"] = remark
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Record.Remark", dc);
        }
        #endregion
        #region 获取记录信息
        public RecordInfoModel GetRecordInfo(int record_id,int domain_id=-1,string domain="")
        {
            if(record_id <=0)
                throw new Exception("必须指定记录ID");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            Dictionary<string, object> dc = new Dictionary<string, object> { ["record_id"] = record_id };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RecordInfoModel>("Record.Info", dc);
        }
        #endregion
        #region 设置记录状态
        public SetRecordStatusRetModel SetRecordStatus(int record_id,string status,int domain_id=-1,string domain="")
        {
            if(record_id<=0)
                throw new Exception("必须指定记录ID");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定域名");
            if(status !="enable" && status !="disable")
                throw new Exception("状态不正确");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["record_id"] = record_id,
                ["status"] = status
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<SetRecordStatusRetModel>("Record.Status", dc);
        }
        #endregion
    }
}
